package com.hs.mod_syslog.interceptcut

import com.flyjingfish.android_aop_annotation.ProceedJoinPoint
import com.flyjingfish.android_aop_annotation.base.BasePointCut
import com.hs.framework.ext.toJson
import com.hs.framework.helper.HsAppHelper
import com.hs.framework.utils.DateUtils
import com.hs.framework.utils.FileUtils
import com.hs.framework.utils.ThreadPoolUtils
import com.hs.mod_syslog.annotation.SysLogIntercept
import com.hs.room.AppDatabase
import com.hs.room.model.sys.SysLog
import com.hs.room.provider.SysLogProvider
import com.hs.room.repository.SysLogRepository
import java.util.Date
import javax.inject.Inject

/**
 * 系统日志 默认处理 不要把注解加到生命周期上
 */
class SysLogInterceptCut : BasePointCut<SysLogIntercept> {
    override fun invoke(
        joinPoint: ProceedJoinPoint,
        annotation: SysLogIntercept //annotation就是你加到方法上的注解
    ): Any? {
        // 在此写你的逻辑
        var startTime = System.currentTimeMillis()
//        println("----------------系统日志-start time ${startTime}-------------------")
        var obj = joinPoint.proceed()
        var endTime = System.currentTimeMillis() - startTime
        ThreadPoolUtils.getExecutorService().execute(
            Runnable {
                    var paramsArg = ""
                    var proceedResult = ""
                    try {
                           joinPoint.args?.let {
                                 paramsArg =joinPoint.targetClass.name+":" + joinPoint.targetMethod.name + ":" + it.toJson()
                            }?:run {
                                 paramsArg =joinPoint.targetClass.name+":" + joinPoint.targetMethod.name
                             }
                            obj?.let {
                                proceedResult = it.toJson()
                            }
                          var v = ""
                          if (annotation.value != null){
                              annotation.value.forEach { v+=it }
                          }
                          var sysLog = SysLog(logName = v?:"系统日志", createTimestamp = System.currentTimeMillis(), createTime = DateUtils.formatDateHHMMSS(
                              Date()
                          ), message =  v?:"系统日志",timeConsuming = endTime,paramsArg = paramsArg, proceedResult = proceedResult, status = 1)
                          SysLogProvider.initSysLog(sysLog)
                  }catch (e:Exception){
                      e.printStackTrace()
                  }
            }
        )
        println("----------------系统日志-start time ${endTime}-------------------")
        return obj
    }
}